home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / bfd / bfd.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  11KB  |  452 lines

  1. /* Generic BFD library interface and support routines.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* $Id: bfd.c,v 1.30 1991/10/17 06:05:18 gnu Exp $ */
  22.  
  23. /*proto*
  24. @section @code{typedef bfd}
  25.  
  26. A BFD is has type @code{bfd}; objects of this type are the cornerstone
  27. of any application using @code{libbfd}. References though the BFD and
  28. to data in the BFD give the entire BFD functionality.
  29.  
  30. Here is the struct used to define the type @code{bfd}.  This contains
  31. the major data about the file, and contains pointers to the rest of
  32. the data.
  33.  
  34. *+++
  35.  
  36. $struct _bfd 
  37. ${
  38.   The filename the application opened the BFD with.
  39.  
  40. $  CONST char *filename;                
  41.  
  42. A pointer to the target jump table.
  43.  
  44. $  struct bfd_target *xvec;
  45.  
  46.  
  47. To avoid dragging too many header files into every file that
  48. includes @file{bfd.h}, IOSTREAM has been declared as a "char *", and MTIME
  49. as a "long".  Their correct types, to which they are cast when used,
  50. are "FILE *" and "time_t".  
  51.  
  52. The iostream is the result of an fopen on the filename.
  53.  
  54. $  char *iostream;
  55.  
  56. Is the file being cached @xref{File Caching}.
  57.  
  58. $  boolean cacheable;
  59.  
  60. Marks whether there was a default target specified when the BFD was
  61. opened. This is used to select what matching algorithm to use to chose
  62. the back end.
  63.  
  64. $  boolean target_defaulted;
  65.  
  66. The caching routines use these to maintain a least-recently-used list of
  67. BFDs (@pxref{File Caching}).
  68.  
  69. $  struct _bfd *lru_prev, *lru_next;
  70.  
  71. When a file is closed by the caching routines, BFD retains state
  72. information on the file here:
  73.  
  74. $  file_ptr where;              
  75.  
  76. and here:
  77.  
  78. $  boolean opened_once;
  79.  
  80. $  boolean mtime_set;
  81. File modified time 
  82.  
  83. $  long mtime;          
  84.  
  85. Reserved for an unimplemented file locking extension.
  86.  
  87. $int ifd;
  88.  
  89. The format which belongs to the BFD.
  90.  
  91. $  bfd_format format;
  92.  
  93. The direction the BFD was opened with
  94.  
  95. $  enum bfd_direction {no_direction = 0,
  96. $                       read_direction = 1,
  97. $                       write_direction = 2,
  98. $                       both_direction = 3} direction;
  99.  
  100. Format_specific flags
  101.  
  102. $  flagword flags;              
  103.  
  104. Currently my_archive is tested before adding origin to anything. I
  105. believe that this can become always an add of origin, with origin set
  106. to 0 for non archive files.  
  107.  
  108. $  file_ptr origin;             
  109.  
  110. Remember when output has begun, to stop strange things happening.
  111.  
  112. $  boolean output_has_begun;
  113.  
  114. Pointer to linked list of sections
  115.  
  116. $  struct sec  *sections;
  117.  
  118. The number of sections 
  119.  
  120. $  unsigned int section_count;
  121.  
  122. Stuff only useful for object files:
  123. The start address.
  124.  
  125. $  bfd_vma start_address;
  126. Used for input and output
  127.  
  128. $  unsigned int symcount;
  129. Symbol table for output BFD
  130.  
  131. $  struct symbol_cache_entry  **outsymbols;             
  132.  
  133. Pointer to structure which contains architecture information
  134.  
  135. $  struct bfd_arch_info *arch_info;
  136.  
  137. Stuff only useful for archives:
  138.  
  139. $  PTR arelt_data;              
  140. $  struct _bfd *my_archive;     
  141. $  struct _bfd *next;           
  142. $  struct _bfd *archive_head;   
  143. $  boolean has_armap;           
  144.  
  145. Used by the back end to hold private data.
  146.  
  147. $  PTR tdata;
  148.  
  149. Used by the application to hold private data
  150.  
  151. $  PTR usrdata;
  152.  
  153. Where all the allocated stuff under this BFD goes (@pxref{Memory Usage}).
  154.  
  155. $  struct obstack memory;
  156. $};
  157.  
  158. *---
  159.  
  160. */
  161. #include "bfd.h"
  162. #include "sysdep.h"
  163. #include "libbfd.h"
  164.  
  165. #undef strerror
  166. extern char *strerror();
  167.  
  168.  
  169. short _bfd_host_big_endian = 0x0100;
  170.         /* Accessing the above as (*(char*)&_bfd_host_big_endian), will
  171.            return 1 if the host is big-endian, 0 otherwise.
  172.            (assuming that a short is two bytes long!!!  FIXME)
  173.            (See HOST_IS_BIG_ENDIAN_P in bfd.h.)  */
  174.  
  175. /** Error handling
  176.     o - Most functions return nonzero on success (check doc for
  177.         precise semantics); 0 or NULL on error.
  178.     o - Internal errors are documented by the value of bfd_error.
  179.         If that is system_call_error then check errno.
  180.     o - The easiest way to report this to the user is to use bfd_perror.
  181. */
  182.  
  183. bfd_ec bfd_error = no_error;
  184.  
  185. char *bfd_errmsgs[] = { "No error",
  186.                         "System call error",
  187.                         "Invalid target",
  188.                         "File in wrong format",
  189.                         "Invalid operation",
  190.                         "Memory exhausted",
  191.                         "No symbols",
  192.                         "No relocation info",
  193.                         "No more archived files",
  194.                         "Malformed archive",
  195.                         "Symbol not found",
  196.                         "File format not recognized",
  197.                         "File format is ambiguous",
  198.                         "Section has no contents",
  199.                         "Nonrepresentable section on output",
  200.                         "#<Invalid error code>"
  201.                        };
  202.  
  203. static 
  204. void 
  205. DEFUN(bfd_nonrepresentable_section,(abfd, name),
  206.          CONST  bfd * CONST abfd AND
  207.          CONST  char * CONST name)
  208. {
  209.   printf("bfd error writing file %s, format %s can't represent section %s\n",
  210.          abfd->filename, 
  211.          abfd->xvec->name,
  212.          name);
  213.   exit(1);
  214. }
  215.  
  216. bfd_error_vector_type bfd_error_vector = 
  217.   {
  218.   bfd_nonrepresentable_section 
  219.   };
  220.  
  221. char *
  222. bfd_errmsg (error_tag)
  223.      bfd_ec error_tag;
  224. {
  225. #ifndef errno
  226.   extern int errno;
  227. #endif
  228.   if (error_tag == system_call_error)
  229.     return strerror (errno);
  230.  
  231.   if ((((int)error_tag <(int) no_error) ||
  232.        ((int)error_tag > (int)invalid_error_code)))
  233.     error_tag = invalid_error_code;/* sanity check */
  234.  
  235.   return bfd_errmsgs [(int)error_tag];
  236. }
  237.  
  238.  
  239. void bfd_default_error_trap(error_tag)
  240. bfd_ec error_tag;
  241. {
  242.   printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag));
  243. }
  244.  
  245. void (*bfd_error_trap)() = bfd_default_error_trap;
  246. void (*bfd_error_nonrepresentabltrap)() = bfd_default_error_trap;
  247.  
  248. void
  249. DEFUN(bfd_perror,(message),
  250.       CONST char *message)
  251. {
  252.   if (bfd_error == system_call_error)
  253.     perror((char *)message);            /* must be system error then... */
  254.   else {
  255.     if (message == NULL || *message == '\0')
  256.       fprintf (stderr, "%s\n", bfd_errmsg (bfd_error));
  257.     else
  258.       fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_error));
  259.   }
  260. }
  261.  
  262.  
  263. /** Symbols */
  264.  
  265. /* returns the number of octets of storage required */
  266.  
  267. unsigned int
  268. get_reloc_upper_bound (abfd, asect)
  269.      bfd *abfd;
  270.      sec_ptr asect;
  271. {
  272.   if (abfd->format != bfd_object) {
  273.     bfd_error = invalid_operation;
  274.     return 0;
  275.   }
  276.  
  277.   return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
  278. }
  279.  
  280. unsigned int
  281. bfd_canonicalize_reloc (abfd, asect, location, symbols)
  282.      bfd *abfd;
  283.      sec_ptr asect;
  284.      arelent **location;
  285.      asymbol **symbols;
  286. {
  287.   if (abfd->format != bfd_object) {
  288.     bfd_error = invalid_operation;
  289.     return 0;
  290.   }
  291.  
  292.   return BFD_SEND (abfd, _bfd_canonicalize_reloc, (abfd, asect, location, symbols));
  293. }
  294.  
  295.  
  296. boolean
  297. bfd_set_file_flags (abfd, flags)
  298.      bfd *abfd;
  299.      flagword flags;
  300. {
  301.   if (abfd->format != bfd_object) {
  302.     bfd_error = wrong_format;
  303.     return false;
  304.   }
  305.  
  306.   if (bfd_read_p (abfd)) {
  307.     bfd_error = invalid_operation;
  308.     return false;
  309.   }
  310.  
  311.   if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
  312.     bfd_error = invalid_operation;
  313.     return false;
  314.   }
  315.  
  316.   bfd_get_file_flags (abfd) = flags;
  317. return true;
  318. }
  319.  
  320.  
  321. void
  322. bfd_set_reloc (ignore_abfd, asect, location, count)
  323.      bfd *ignore_abfd;
  324.      sec_ptr asect;
  325.      arelent **location;
  326.      unsigned int count;
  327. {
  328.   asect->orelocation  = location;
  329.   asect->reloc_count = count;
  330. }
  331.  
  332. void
  333. bfd_assert(file, line)
  334. char *file;
  335. int line;
  336. {
  337.   printf("bfd assertion fail %s:%d\n",file,line);
  338. }
  339.  
  340.  
  341. /*proto* bfd_set_start_address
  342.  
  343. Marks the entry point of an output BFD. Returns @code{true} on
  344. success, @code{false} otherwise.
  345.  
  346. *; PROTO(boolean, bfd_set_start_address,(bfd *, bfd_vma));
  347. */
  348.  
  349. boolean
  350. bfd_set_start_address(abfd, vma)
  351. bfd *abfd;
  352. bfd_vma vma;
  353. {
  354.   abfd->start_address = vma;
  355.   return true;
  356. }
  357.  
  358.  
  359. /*proto*  bfd_get_mtime
  360.  
  361. Return cached file modification time (e.g. as read from archive header
  362. for archive members, or from file system if we have been called
  363. before); else determine modify time, cache it, and return it.  
  364.  
  365. *; PROTO(long, bfd_get_mtime, (bfd *));
  366.  
  367. */
  368.  
  369. long
  370. bfd_get_mtime (abfd)
  371.      bfd *abfd;
  372. {
  373.   FILE *fp;
  374.   struct stat buf;
  375.  
  376.   if (abfd->mtime_set)
  377.     return abfd->mtime;
  378.  
  379.   fp = bfd_cache_lookup (abfd);
  380.   if (0 != fstat (fileno (fp), &buf))
  381.     return 0;
  382.  
  383.   abfd->mtime_set = true;
  384.   abfd->mtime = buf.st_mtime;
  385.   return abfd->mtime;
  386. }
  387.  
  388. /*proto* stuff
  389.  
  390. *+
  391.  
  392. #define bfd_sizeof_headers(abfd, reloc) \
  393.      BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
  394.  
  395. #define bfd_find_nearest_line(abfd, section, symbols, offset, filename_ptr, func, line_ptr) \
  396.      BFD_SEND (abfd, _bfd_find_nearest_line,  (abfd, section, symbols, offset, filename_ptr, func, line_ptr))
  397.  
  398. #define bfd_debug_info_start(abfd) \
  399.         BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
  400.  
  401. #define bfd_debug_info_end(abfd) \
  402.         BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
  403.  
  404. #define bfd_debug_info_accumulate(abfd, section) \
  405.         BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
  406.  
  407. #define bfd_stat_arch_elt(abfd, stat) \
  408.         BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
  409.  
  410. #define bfd_coff_swap_aux_in(a,e,t,c,i) \
  411.         BFD_SEND (a, _bfd_coff_swap_aux_in, (a,e,t,c,i))
  412.  
  413. #define bfd_coff_swap_sym_in(a,e,i) \
  414.         BFD_SEND (a, _bfd_coff_swap_sym_in, (a,e,i))
  415.  
  416. #define bfd_coff_swap_lineno_in(a,e,i) \
  417.         BFD_SEND ( a, _bfd_coff_swap_lineno_in, (a,e,i))
  418.  
  419. #define bfd_set_arch_mach(abfd, arch, mach)\
  420.         BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
  421.  
  422. #define bfd_coff_swap_reloc_out(abfd, i, o) \
  423.         BFD_SEND (abfd, _bfd_coff_swap_reloc_out, (abfd, i, o))
  424.  
  425. #define bfd_coff_swap_lineno_out(abfd, i, o) \
  426.         BFD_SEND (abfd, _bfd_coff_swap_lineno_out, (abfd, i, o))
  427.  
  428. #define bfd_coff_swap_aux_out(abfd, i, t,c,o) \
  429.         BFD_SEND (abfd, _bfd_coff_swap_aux_out, (abfd, i,t,c, o))
  430.  
  431. #define bfd_coff_swap_sym_out(abfd, i,o) \
  432.         BFD_SEND (abfd, _bfd_coff_swap_sym_out, (abfd, i, o))
  433.  
  434. #define bfd_coff_swap_scnhdr_out(abfd, i,o) \
  435.         BFD_SEND (abfd, _bfd_coff_swap_scnhdr_out, (abfd, i, o))
  436.  
  437. #define bfd_coff_swap_filehdr_out(abfd, i,o) \
  438.         BFD_SEND (abfd, _bfd_coff_swap_filehdr_out, (abfd, i, o))
  439.  
  440. #define bfd_coff_swap_aouthdr_out(abfd, i,o) \
  441.         BFD_SEND (abfd, _bfd_coff_swap_aouthdr_out, (abfd, i, o))
  442.  
  443. *-
  444.  
  445. */
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.